﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JobbJakt;

namespace JobbJaktWeb
{
    public partial class EducationWebForm : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] == null)
                {
                    Response.Redirect("Login.aspx");
                }
                string username = Session["UserName"] as string;
                    FillListView(username);
                //FillListView("Dante");
                Array Values = System.Enum.GetValues(typeof(EducationLevel));

               
            }
         
         
          



        }

        protected void FillListView(string username)
        {
            //var myList = Education.GetEducations(username);
          
            ListViewEducation.DataSource = Education.GetEducations(username);
            ListViewEducation.DataBind();
        }

        public void DeleteEducation(object sender, ListViewDeleteEventArgs e)
        {
            var autoid = int.Parse(ListViewEducation.DataKeys[e.ItemIndex].Value.ToString());
            int rows = 0;
            //var item = ListViewEducation.Items[e.ItemIndex] ;
           
            //var obj = item.DataItem as Education;

            rows = Education.DeleteEducation(autoid);

            ListViewEducation.EditIndex = -1;
            string username = Session["UserName"] as string;
            FillListView(username);
        }
        public void InsertEducation(object sender, ListViewInsertEventArgs e)
        {
           
            ListViewItem item = e.Item;
            TextBox tName = (TextBox)item.FindControl("TextBoxEducationName");
            TextBox tStartDate = (TextBox)item.FindControl("TextBoxEducationStartDate");
            TextBox tEndDate = (TextBox)item.FindControl("TextBoxEndEducationDate");
            ListBox lEducationLevel = (ListBox)item.FindControl("ListBoxEducationLevel");
            DropDownList dExamination = (DropDownList)item.FindControl("DropDownListExamination");

        //     int EducationID 
        //string StartDate 
        //DateTime EndDate 
        //EducationLevel EducationLevel 
        //string Name 
        // bool Examination 
        //string UserName 

            
                var itemObj =
                new Education
                {

                    EducationLevel = (EducationLevel)lEducationLevel.SelectedIndex,
                    EndDate = DateTime.Parse(tEndDate.Text),
                    Examination = dExamination.SelectedIndex == 0,
                    Name = tName.Text.Trim(),
                    StartDate = tStartDate.Text.Trim(),
                    UserName = Session["UserName"] as string
                  

                };

                //  bool Exam = dExamination.SelectedIndex == 0 ? true : false;
              //Education   obj = new Education(tStartDate.ToString().Trim(),DateTime.Parse(tEndDate.ToString(),(EducationLevel)lEducationLevel.SelectedItem ) ;
                
                int rows = Education.InsertEducation(itemObj);
                ListViewEducation.EditIndex = -1;
                string username = Session["UserName"] as string;
                FillListView(username);
           

        }

        public void UpdateEducation(object sender, ListViewUpdateEventArgs e)
        {
            int autoid = int.Parse(ListViewEducation.DataKeys[e.ItemIndex].Value.ToString());

            ListViewItem item = ListViewEducation.Items[e.ItemIndex];
            TextBox tName = (TextBox)item.FindControl("TextBoxEducationName");
            TextBox tStartDate = (TextBox)item.FindControl("TextBoxEducationStartDate");
            TextBox tEndDate = (TextBox)item.FindControl("TextBoxEndEducationDate");
            ListBox lEducationLevel = (ListBox)item.FindControl("ListBoxEducationLevel");
            DropDownList dExamination = (DropDownList)item.FindControl("DropDownListExamination");

            Education itemObj = (Education)item.DataItem;
            itemObj.Name = tName.Text.Trim();
            itemObj.StartDate = tStartDate.Text.Trim();
            itemObj.EndDate = DateTime.Parse(tEndDate.Text);
            itemObj.EducationLevel = (EducationLevel)lEducationLevel.SelectedIndex;
            itemObj.Examination = dExamination.SelectedIndex == 0;
            itemObj.UserName = Session["UserName"] as string;
            itemObj.EducationID = autoid;

            //var itemObj =
            // new Education
            // {

            //     EducationLevel = (EducationLevel)lEducationLevel.SelectedIndex,
            //     EndDate = DateTime.Parse(tEndDate.Text),
            //     Examination = dExamination.SelectedIndex == 0,
            //     Name = tName.Text.Trim(),
            //     StartDate = tStartDate.Text.Trim(),
            //     UserName = Session["UserName"] as string,
            //     EducationID = autoid


            // };
          
                int rows = Education.UpdateEducation(itemObj);
                ListViewEducation.EditIndex = -1;
                string username = Session["UserName"] as string;
                FillListView(username);
            
        }

        public void CancelEditEducation(object sender, ListViewCancelEventArgs e)
        {
            ListViewEducation.EditIndex = -1;

            string username = Session["UserName"] as string;
            FillListView(username);
        }

        public void EditEducation(object sender, ListViewEditEventArgs e)
        {
            ListViewEducation.EditIndex = e.NewEditIndex;
            string username = Session["UserName"] as string;
            FillListView(username);
           

        }

        //private string GetEnum(EducationLevel level)
        //{
        //    switch (level)
        //    {
        //        case EducationLevel.Gymnasium:
        //            return EducationLevel.Gymnasium.ToString();
                   
        //        case EducationLevel.Grundskola:
        //            return EducationLevel.Grundskola.ToString();
                   
        //        case EducationLevel.Högskola:
        //            return EducationLevel.Högskola.ToString();
        //        case EducationLevel.Universitet:
        //            return EducationLevel.Universitet.ToString();
        //        case EducationLevel.Yrkeshögskola:
        //            return EducationLevel.Yrkeshögskola.ToString();
                

        //    }

        //}
    }
}